/**
 * 
 */
package com.game.jumpdino.logic.visual;

import com.game.jumpdino.R;
import com.game.jumpdino.logic.Playground;
import com.game.jumpdino.logic.VisualTurnedListener;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;

/**
 * @author HugoGu
 *
 */
public class Umbrella extends Holder {
	private Drawable vUmbrella;
	private Point hookPoint = new Point(45, -20);
	private Point hookPointTurn = new Point(-15, -20);
	private long jumpTime;
	
	@Override
	public void onHit(final VisualBase visual, final Playground ground){
		if (!ground.avatar.isHasTool() && this.getImage() != vUmbrella){
			ground.avatar.setHasTool(true);
			super.onHit(visual, ground);
			VisualBase.setMinSpeedUp(-18);
			visual.setG(3.0f);
			this.setImage(vUmbrella);
			jumpTime = System.currentTimeMillis();
			final VisualTurnedListener lisenter = new VisualTurnedListener(){
				public void onTurned(VisualBase who) {
					if (ground.avatar.getTurned()){
						float targetX = who.getPosition().left + hookPointTurn.x;
						float targetY = who.getPosition().top + hookPointTurn.y;
						getPosition().offset(Math.round(targetX - getPosition().left), Math.round(targetY - getPosition().top));
					} else {
						float targetX = who.getPosition().left + hookPoint.x;
						float targetY = who.getPosition().top + hookPoint.y;
						getPosition().offset(Math.round(targetX - getPosition().left), Math.round(targetY - getPosition().top));
					}
				}
			};
			ground.avatar.addTurnedListener(lisenter);			
			if (ground.avatar.getTurned()){
				float targetX = visual.getPosition().left + hookPointTurn.x;
				float targetY = visual.getPosition().top + hookPointTurn.y;
				move(targetX - getPosition().left, targetY - getPosition().top);
			} else {
				float targetX = visual.getPosition().left + hookPoint.x;
				float targetY = visual.getPosition().top + hookPoint.y;
				move(targetX - getPosition().left, targetY - getPosition().top);
			}
			this.follow(visual);
	        Thread splashTread = new Thread() {
	            @Override
	            public void run() {
	                try {
	                	Thread.sleep(15000);
	                } catch(InterruptedException e) {
	                    // do nothing
	                } finally {
		                if (ground.avatar == visual){
		                	unFollow(visual);
		                	((Avatar)visual).removeTurnedListener(lisenter);
		                	setG(3.0f);
		                	((Avatar)visual).setHasTool(false);
							VisualBase.setMinSpeedUp(VisualBase.defaultJumpSpeed);
							visual.setG(Avatar.defaultG);
	                	}
	                }
	            }
	        };
	        splashTread.start();
		}
	}
	
	@Override
	public void drawOn(Canvas c){
		if (jumpTime > 0 && this.getAttachTo() != null){
			long nowJumpTime = System.currentTimeMillis();
			long duration = nowJumpTime - jumpTime;
			if (duration > 10000){
				// Flash by half seconds.
				if (duration % 1000 < 500){
					super.drawOn(c);
				}
			} else {
				super.drawOn(c);
			}
		} else {
			super.drawOn(c);
		}
	}
	
	@Override
	public void onInitialized(Context context){
		super.onInitialized(context);
		vUmbrella = context.getResources().getDrawable(R.drawable.umbrellav);
	}
}
